package com.ferry.config.client.loader;

import com.ferry.config.client.monitor.ConfigMonitor;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

/**
 * Created by daiyong
 */
public abstract class AbstractConfigLoader implements ConfigLoader{

	int CONNECT_TIMEOUT = 3000; // 3 seconds

	int READ_TIMEOUT = 60000; //

	int CONNECTION_REQUEST_TIMEOUT = 10000; // 1s

	protected CloseableHttpClient httpClient;

	protected String namespace;

	protected String env;

	protected ConfigMonitor configMonitor;

	public AbstractConfigLoader(ConfigMonitor configMonitor, String namespace, String env) {
		this.configMonitor = configMonitor;
		this.namespace = namespace;
		this.env = env;
	}

	protected void initHttp() {
		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
				.setSocketTimeout(READ_TIMEOUT)
				.setConnectTimeout(CONNECT_TIMEOUT).build();
		httpClient = HttpClients.custom()
				.setMaxConnPerRoute(1)
				.setMaxConnTotal(2)
				.setDefaultSocketConfig(SocketConfig.custom().setSoLinger(0).build())
				.setDefaultRequestConfig(requestConfig).build();
	}

}
